import Vue from 'vue';
import Vuex from 'vuex';
import { sendCode } from 'services/sms';
import {
  login,
  phoneLogin,
  logout,
  regist,
  selectBeeUser,
} from '../services/beeUser';
import price from './modules/price';
import wechat from './modules/wechat';
import getters from './getters'

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    userInfo: window.__INITIAL_STATE__.userInfo
  },
  mutations: {
    SET_USER_INFO: (state, userInfo) => {
      // 设置用户信息
      state.userInfo = userInfo;
    }
  },
  actions: {
    /*
    * @author JinHang
    * @description 验证码及密码登录
    * @param { object } userinfo 是一个带用户名，密码|短信验证码，登录模式的对象
    * @return promise对象
    * @date 2018/6/28 15:15
    * */
    userLogin({ commit }, loginInfo) {

      return new Promise((resolve, reject) => {

        const { loginMode, mobileNo, loginPasswordOrSmsCode } = loginInfo;

        if (loginMode === 'smsCode') {

          phoneLogin({ mobileNo, code: loginPasswordOrSmsCode, codeType: 'login' }).then((response) => {

            const { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo, name, idNumber } = response.object;

            commit('SET_USER_INFO', { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo, name, idNumber  });

            resolve(response);

          }).catch((e) => {

            reject(e);

          });

        } else if (loginMode === 'password') {

          login({ mobileNo, loginPassword: loginPasswordOrSmsCode }).then((response) => {

            const { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo, name, idNumber } = response.object;

            commit('SET_USER_INFO', { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo, name, idNumber });

            resolve(response);

          }).catch((e) => {

            reject(e);

          });

        }

      });

    },
    /*
    * @author JinHang
    * @description 获取验证码
    * @param {string} mobileNo 电话号码
    * @return promise对象
    * @date 2018/6/28 15:18
    * */
    getSmsCode({ commit }, smsCodeData) {

      return new Promise((resolve, reject) => {

        const { mobileNo, codeType } = smsCodeData;

        sendCode({ mobileNo, codeType }).then((response) => {

          resolve(response);

        }).catch((e) => {

          reject(e);

        });

      });

    },
    /*
    * @author JinHang
    * @description 注册用户
    * @date 2018/8/1 16:37
    * */
    register({ commit }, registerData) {

      return new Promise((resolve, reject) => {

        const { mobileNo, code, codeType } = registerData;

        regist({ mobileNo, code, codeType }).then((response) => {

          const { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo, name, idNumber } = response.object;

          commit('SET_USER_INFO', { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo, name, idNumber });

          resolve(response);

        }).catch((e) => {

          reject(e);

        });

      });

    },
    logout({ commit }, userInfo) {

      return new Promise((resolve, reject) => {

        const { userNo } = userInfo;

        logout({ userNo }).then((response) => {

          commit('SET_USER_INFO', null);

          resolve(response);

        }).catch((e) => {

          reject(e);

        });

      });

    },
    refreshUserInfo({ commit }, userInfo) {

      return new Promise((resolve, reject) => {

        const { userNo } = userInfo;

        selectBeeUser({ userNo }).then((response) => {

          const { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo, name, idNumber} = response.object;

          commit('SET_USER_INFO', { bindBeeUser, birthday, createTime, headPic, mobileNo, nickName, sex, userNo, name, idNumber});

          resolve(response);

        }).catch((e) => {

          reject(e);

        });

      });

    }
  },
  modules: {
    price,
    wechat,
  },
  getters
});
